package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SrcTxnAuthCfgMapper;
import cc.rengu.oltp.service.common.entity.SrcTxnAuthCfg;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 交易配置表操作方法实现类
 * Created by 王成 on 2017/11/22.
 */
public class SrcTxnAuthCfgMapperImpl implements SrcTxnAuthCfgMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public SrcTxnAuthCfgMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public List<SrcTxnAuthCfg> loadSrcTxnAuthCfgInfo(String dbPoolCacheNodeName) throws Exception {
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCacheNodeName, AppParamConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadSrcTxnAuthCfgInfo");
        int pageIndex = 0;
        List<SrcTxnAuthCfg> srcTxnAuthCfgList = null;
        while (true) {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex++);
            pageInfo.setPageSize(DbsUtil.MAX_FETCH_NUM);
            PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, new SrcTxnAuthCfg(), SrcTxnAuthCfg.class, pageInfo);
            if (null != pageResult) {
                if (null == srcTxnAuthCfgList) {
                    srcTxnAuthCfgList = new ArrayList<>();
                }
                srcTxnAuthCfgList.addAll(pageResult.getResult().stream().map(item -> (SrcTxnAuthCfg) (item)).collect(Collectors.toList()));
            } else {
                rglog.warn("数据库连接池【{}】交易配置信息为空或者查询失败!", dbPoolName);
                break;
            }
            if (pageIndex >= pageInfo.getTotalPage()) {
                break;
            }
        }
        return srcTxnAuthCfgList;
    }

    @Override
    public List<SrcTxnAuthCfg> selectSrcTxnAuthCfgInfo(String instId, String msgSrcId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSrcTxnAuthCfgInfo");
        SrcTxnAuthCfg srcTxnAuthCfg = new SrcTxnAuthCfg();
        srcTxnAuthCfg.setInstId(instId);
        if (null != msgSrcId && !msgSrcId.isEmpty()) {
            srcTxnAuthCfg.setMsgSrcId(msgSrcId);
        }
        int pageIndex = 0;
        List<SrcTxnAuthCfg> srcTxnAuthCfgList = null;
        while (true) {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex++);
            pageInfo.setPageSize(DbsUtil.MAX_FETCH_NUM);
            PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, srcTxnAuthCfg, SrcTxnAuthCfg.class, pageInfo);
            if (null != pageResult) {
                if (null == srcTxnAuthCfgList) {
                    srcTxnAuthCfgList = new ArrayList<>();
                }
                srcTxnAuthCfgList.addAll(pageResult.getResult().stream().map(item -> (SrcTxnAuthCfg) (item)).collect(Collectors.toList()));
            } else {
                rglog.warn("根据法人行标识及渠道标识获取所有通道权限配置信息失败，instId:<{}>,msgSrcId:<{}>", instId, msgSrcId);
                break;
            }
            if (pageIndex >= pageInfo.getTotalPage()) {
                break;
            }
        }
        return srcTxnAuthCfgList;
    }

    @Override
    public SrcTxnAuthCfg selectSrcTxnAuthCfgByPrimaryKey(String instId, String msgSrcId, String transChannelId, String txnNum, String bizType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSrcTxnAuthCfgByPrimaryKey");
        SrcTxnAuthCfg srcTxnAuthCfg = new SrcTxnAuthCfg();
        srcTxnAuthCfg.setInstId(instId);
        srcTxnAuthCfg.setMsgSrcId(msgSrcId);
        srcTxnAuthCfg.setTransChannelId(transChannelId);
        srcTxnAuthCfg.setTxnNum(txnNum);
        srcTxnAuthCfg.setBizType(bizType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, srcTxnAuthCfg, srcTxnAuthCfg);
        if (0 != iReturnCode) {
            rglog.warn("查询渠道交易权限配置信息失败,instId:<{}>,msgSrcId:<{}>,transChannelId:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>", instId, msgSrcId, transChannelId, txnNum, bizType, iReturnCode);
            return null;
        }
        return srcTxnAuthCfg;
    }

    @Override
    public int updateSrcTxnAuthCfgByPrimaryKey(SrcTxnAuthCfg srcTxnAuthCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSrcTxnAuthCfgByPrimaryKey");
        srcTxnAuthCfg.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, srcTxnAuthCfg, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新渠道交易权限配置信息失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>",
                    srcTxnAuthCfg.getInstId(), srcTxnAuthCfg.getMsgSrcId(), srcTxnAuthCfg.getTxnNum(), srcTxnAuthCfg.getBizType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int insertSrcTxnAuthCfg(SrcTxnAuthCfg srcTxnAuthCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSrcTxnAuthCfg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, srcTxnAuthCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记渠道交易权限配置信息失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>",
                    srcTxnAuthCfg.getInstId(), srcTxnAuthCfg.getMsgSrcId(), srcTxnAuthCfg.getTxnNum(), srcTxnAuthCfg.getBizType(), iReturnCode);
        }
        return iReturnCode;
    }
}
